package com.aaa.controller.jl;

import com.aaa.pojo.Menu;
import com.aaa.pojo.jl.ResltBean;

import com.aaa.service.jl.MenuService;

import com.aaa.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;
    ResltBean resltBean = new ResltBean();
    @RequestMapping("/queryAllMenu")
    public ResltBean queryAllMenu(){
        int count = menuService.count();
        List<Menu> menuList = menuService.queryAllMenu();
        resltBean.setCode(0);
        resltBean.setMsg("");
        resltBean.setCount(count);
        resltBean.setData(menuList);
        return  resltBean;
    }
    @RequestMapping("/queryAllMenus")
    public List queryAllMenus(){

        List<Menu> menuList = menuService.queryAllMenu();

        return TreeUtils.fromMenuListToTreeList(menuList);
    }
    @RequestMapping("/insertMenu")
    public ResltBean insertMenu(Menu menu){
        System.out.println(menu);
        int insert = menuService.insert(menu);
        resltBean.setCode(0);
        resltBean.setMsg("");
        resltBean.setCount(insert);
        resltBean.setData(menu);
       // System.out.println(menu);
        return  resltBean;
    }
    @RequestMapping("/findParentNameId")
    public ResltBean findParentNameId(Integer menuId){
        Menu parentNameId = menuService.findParentNameId(menuId);
        resltBean.setCode(0);
        resltBean.setMsg("");
        resltBean.setCount(100);
        resltBean.setData(parentNameId);
        return  resltBean;
    }
    @RequestMapping("/updateMenu")
    public ResltBean updateMenu(Menu menu ){

        menuService.update(menu);
        resltBean.setCode(0);
        resltBean.setMsg("");
        resltBean.setCount(1);
        resltBean.setData(menu);
        return  resltBean;
    }
    @RequestMapping("/deleteById")
    public ResltBean deleteById(String menuId){
        System.out.println(menuId);
        menuService.deleteById(menuId);
        resltBean.setCode(0);
        resltBean.setMsg("");
        resltBean.setCount(1);
        resltBean.setData(menuId);
        return  resltBean;
    }

}
